<?php
/*
* Plugin Name: CDIA Portfolio
* Plugin URI: http://www.cdiabu.com
* Description: Adds a portfolio post type that allows for easy creation of a web development portfolio
* Author: Bryan Russo
*/
//Defines
define( 'CDIA_PORT_OPTIONS_NAME', 'cdia_portfolio_settings' );
define( 'CDIA_PORT_META_KEY', 'cdia_portfolio_details' );

//Includes
require_once( 'inc/portfolio.php' );
require_once( 'inc/portfolio-template-tags.php' );
 
//On plugin activation
register_activation_hook( __FILE__, 'cdia_port_on_activate');

//On plugin deactivation
register_deactivation_hook( __FILE__, 'cdia_port_on_deactivate');

 //Wordpress hooks
 add_action('init','cdia_port_init');
 add_action('init','cdia_port_admin_init');
 add_action('admin_menu','cdia_port_admin_menu');
 add_action( 'add_meta_boxes', 'cdia_port_add_metabox', 10, 2);
 add_action( 'save_post', 'cdia_port_on_post_save', 10, 2);


//callback function run on plugin activation
function cdia_port_on_activate(){
	
	if ( version_compare( get_bloginfo( 'version' ). '3.0', '<' ) ) {
		deactivate_plugins(__FILE__);
	} else {
		
		flush_rewrite_rules();
		
		}
}


//callback function run on plugin deactivation
function cdia_port_on_deactivate(){
	
		flush_rewrite_rules();
	
	}
	
//callback function for init hook
function cdia_port_init(){
	
	//Register the port
	cdia_port_register_portfolio();
	
	}

//callback function for admin_init hook
function cdia_port_admin_init(){
	
	}
	
//callback function for admin_init hook
function cdia_port_admin_menu(){
	
	add_options_page( 'Portfolio Settings',      //title displayed on options page 
					  'Porfolio Settings',       //menu item label name
					  'manage_options',          //user capability required
					  'cdia-portfolio-settings', //URL page value
					  'cdia_part_settings_page' //callback function for page display
 	);
	
	}
	
//Render admin settings page
function cdia_part_settings_page(){
	
	$portfolio_settings = array( 'email' => '', 'summary' => '');
	$errors = '';
	
	if( isset( $_POST['cdia_port_save']) ) {
		
		if (!wp_verify_nonce( $_POST['_wpnonce'], 'update-portfolio-settings')) {
			wp_die('cheatin, uh?');
			}
		
			$portfolio_settings['email'] = $_POST['cdia_port_email'];
			$portfolio_settings['summary'] = strip_tags( $_POST['cdia_port_summary']);
			
			if( !is_email($portfolio_settings['email'] )) {
				$portfolio_settings['email'] = '';
				$errors = '<p>Please insert a valid email address</p><br />';		
				}
				
			if( empty( $errors )){	
				update_option( CDIA_PORT_OPTIONS_NAME, $portfolio_settings );
			}
		} else {
			
			$portfolio_settings = get_option( CDIA_PORT_OPTIONS_NAME );
			
			if($portfolio_settings == false) {
				$portfolio_settings = array('email' => '', 'summary' => '');
				}
			}
	
 	//Display settings page
	include 'interface/portfolio-settings.php';
	
	}
	


//Create custom meta-boxes

	function cdia_port_add_metabox( $post_type, $post ) {
	
		add_meta_box( 'cdia-port-meta', //CSS of the <div> that wraps our metabox
					  'Portfolio Details', //Title of the metabox
					  'cdia_port_display_metabox', //Callback for rendering metabox content
					  'portfolio', //Post type for meta box display
					  'normal', //Context  - part of page where meta will show
					  'high' //priority of metabox display related to other metaboxes on the page
					);
	
	}
	
	function cdia_port_display_metabox( $post ) {
		
		$portfolio_input = get_post_meta( $post->ID, CDIA_PORT_META_KEY, true);
		
		if( $portfolio_input == false){
			
			$portfolio_input = array();
			
			$portfolio_input['client_name'] = '';
			$portfolio_input['year'] = '2011';
			$portfolio_input['url'] = 'http://';
			$portfolio_input['cms'] = 'WordPress';
			}		
			
			extract ($portfolio_input);
		//display the form
		include 'interface/portfolio-metabox.php';
	}
	
	//run when a post is saved	
	function cdia_port_on_post_save( $post_id, $post ){
		
		//bail on autosave
		if ($post->post_type == 'revision' ) {
			return;
	}
			
		if(isset ($_POST['cdia_portfolio']) ) {
			
			$portfolio_input = $_POST['cdia_portfolio'];
			
			$portfolio_input['client_name'] = strip_tags ($portfolio_input['client_name'] );
			$portfolio_input['year'] = absint( $portfolio_input['year'] );
			$portfolio_input['url'] = esc_url( $portfolio_input['url'] );
			
			update_post_meta( $post_id, CDIA_PORT_META_KEY, $portfolio_input );
			
			}
		
		}
?>